// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KinectHorizontalDepthProfileAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Runtime.Perception
{
    using System;
    using System.IO;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Navigation.Perception;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Runtime.Cameras;

    /// <summary>
    /// Generates a horizontal depth profile from Kinect data
    /// </summary>
    [DataContract]
    public class KinectHorizontalDepthProfileAgent : ConsumerProducerAgent<DepthCameraAgentMessage<KinectDepthCameraParameters>, SimpleAgentMessage<KinectHorizontalDepthProfile>>
    {
        /// <summary>
        /// The name of the file containing the serialized depth camera intrinsic parameters
        /// </summary>
        [DataMember]
        private string intrinsicParametersFolder;

        /// <summary>
        /// The depth camera intrinsic parameters
        /// </summary>
        private DepthCameraIntrinsicParameters intrinsicParameters;

        /// <summary>
        /// Initializes a new instance of the KinectHorizontalDepthProfileAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="intrinsicParametersFolder">The file containing the intrinsic parameters of the depth camera</param>
        /// <param name="producer">Producer to subscribe to</param>
        public KinectHorizontalDepthProfileAgent(string name, string intrinsicParametersFolder, IProducer<DepthCameraAgentMessage<KinectDepthCameraParameters>> producer)
            : base(name, producer.Name)
        {
            this.intrinsicParametersFolder = intrinsicParametersFolder;
        }

        /// <summary>
        /// Initializes a deserialized agent instance, in lieu of a constructor.
        /// </summary>
        /// <param name="locator">The agent locator that gives access to other agents in the pipeline</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            string intrinsicsFileName = Path.Combine(this.intrinsicParametersFolder, "DepthCameraIntrinsics.xml");
            if (File.Exists(intrinsicsFileName))
            {
                this.intrinsicParameters = Serializer.Load<DepthCameraIntrinsicParameters>(intrinsicsFileName);
                Console.WriteLine("Loaded camera intrinsic parameters file: {0}, ({1})", intrinsicsFileName, File.GetLastWriteTime(intrinsicsFileName));
            }
            else
            {
                ////throw new FileNotFoundException("Depth camera intrinsic parameters file not found: " + intrinsicParametersFileName);
                Console.WriteLine("Depth camera intrinsic parameters file not found: " + intrinsicsFileName);
                this.intrinsicParameters = new DepthCameraIntrinsicParameters();
            }
        }

        /// <summary>
        /// Called when the agent receives a message.
        /// </summary>
        /// <param name="message">The received message.</param>
        public override void Receive(DepthCameraAgentMessage<KinectDepthCameraParameters> message)
        {
            KinectHorizontalDepthProfile hdp = new KinectHorizontalDepthProfile((ImageFrameDepth)message.ImageFrame, message.Parameters.Pose, 0.0f, this.intrinsicParameters);
            this.Publisher.Post(new SimpleAgentMessage<KinectHorizontalDepthProfile>(hdp, message));
        }
    }
}
